package gui.properties;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.MetroModel;
import model.objects.EdiStation;

import com.nightpiter.model.exceptions.TimeFormatException;
import com.nightpiter.model.objects.Coordinates;

import com.nightpiter.model.objects.Time;

public class StationPropertiesPanel extends FlowPanel{

	private EdiStation station;
	
	private JTextField nameField;
	private JTextField pointField ;	
	private OpenTimeField openTimeField; 
	private CloseTimeField closeTimeField;
	private LocationField locationField;
	private LabelField labelField;
	
		
	public StationPropertiesPanel(EdiStation t) {
		super(t.getName());
		this.station = t;
		this.setPanelComponents(this.initPanelComponents());		
		this.setPrVisible(false);
	}

	private List<Component> initPanelComponents() {
		final EdiStation t = station;
		List<Component> comps = new LinkedList<Component>();		
		JPanel namePanel = new JPanel();
		JPanel pointPanel = new JPanel(); 
		JPanel openTimePanel = new JPanel();
		JPanel closeTimePanel = new JPanel();
		JPanel locationPanel = new JPanel();
		JPanel labelPanel = new JPanel(); 
		
		nameField = new NameField();
		pointField = new PointField();	
		openTimeField = new OpenTimeField();
		closeTimeField = new CloseTimeField();
		locationField = new LocationField();
		labelField = new LabelField();
		
		namePanel.setLayout(new GridLayout(0,2));
		pointPanel.setLayout(new GridLayout(0,2));
		openTimePanel.setLayout(new GridLayout(0,2));
		closeTimePanel.setLayout(new GridLayout(0,2));
		locationPanel.setLayout(new GridLayout(0,2));
		labelPanel.setLayout(new GridLayout(0,2));
		
		namePanel.add(new JLabel("Name:"));
		
		namePanel.add(nameField);
		
		
		pointPanel.add(new JLabel("Position:"));			 
		pointPanel.add(pointField);
		
		openTimePanel.add(new JLabel("Open time:"));
		openTimePanel.add(openTimeField);
		
		closeTimePanel.add(new JLabel("Close time:"));		
		closeTimePanel.add(closeTimeField);

		locationPanel.add(new JLabel("Location:"));		
		locationPanel.add(locationField);
		
		labelPanel.add(new JLabel("Label position:"));		
		labelPanel.add(labelField);
		
		comps.add(namePanel);
		comps.add(pointPanel);
		comps.add(openTimePanel);
		comps.add(closeTimePanel);
		comps.add(locationPanel);		
		
		return comps;
	}
	
	@Override
	public void updateData() {		
		
		//TODO update ALL data, not only name and position
		String text = station.getPosition().toString();
		this.pointField.setText(text);
		this.nameField.setText(station.getName());
		this.setButtonName(station.getName());
		
		
	}
	
	private Point parsePoint(String str) {
		str = str.substring(1,str.length()-1);
		int x = Integer.parseInt(str.substring(0,str.indexOf(';')));
		int y = Integer.parseInt(str.substring(str.indexOf(';')+1));
		return new Point(x,y);
	}
	private Coordinates parsePosition(String str) {
		Point p = parsePoint(str);
		return new Coordinates(p.x,p.y);
	}
	private Coordinates parseLocation(String str) {
		return null;
	}

	
	
	class NameField extends JTextField {
		public NameField() {
			//final JTextField nameField = this;
			setText(station.getName());			
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setStationName(station, getText());
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
	}
	
	class PointField extends JTextField {
		public PointField() {
			setText((station.getPosition()!=null) ? station.getPosition().toString() : "(0;0)");
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						moveStation();
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		private void moveStation() {
			try {
				model.MetroModel.getInstance().moveStation(station, parsePosition(PointField.this.getText()));
			} catch(NumberFormatException nfe) {
				return;
			}
		}
	}
	
	class LabelField extends JTextField {
		public LabelField() {
			setText("("+station.getLabelX()+";"+ station.getLabelY()+")");
						
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						moveLabel();
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		private void moveLabel() {
			try {
				model.MetroModel.getInstance().moveStation(station, parsePosition(pointField.getText()));
			} catch(NumberFormatException nfe) {
				return;
			}
		}
	}
	
	class LocationField extends JTextField {
		public LocationField() {
			final JTextField tf = this;
			setText((station.getLocation()!=null) ? station.getLocation().toString() : "(0;0)");
						
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						MetroModel.getInstance().SetStationLocation(station, parseLocation(tf.getText()));
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
	}
	
	class OpenTimeField extends JTextField {
		public OpenTimeField() {
			final JTextField tf = this;
			setText((station.getOpenTime()!=null) ? station.getOpenTime().toString() : "00:00");
						
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						setTime(tf);
						
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		private void setTime(final JTextField tf) {
			try {
				Time time = new Time(tf.getText());
				MetroModel.getInstance().setStationTime(station, time,  station.getCloseTime());
			} catch (TimeFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	class CloseTimeField extends JTextField {
		public CloseTimeField() {
			final JTextField tf = this;
			
			setText((station.getCloseTime()!=null) ? station.getCloseTime().toString() : "00:00");
						
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						setTime(tf);
						
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
	}
	private void setTime(final JTextField tf) {
		try {
			Time time = new Time(tf.getText());
			MetroModel.getInstance().setStationTime(station, station.getOpenTime(), time);
		} catch (TimeFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}


